<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="商品条码" prop="shangpinId">
        <el-input
          v-model="queryParams.shangpinId"
          placeholder="请输入商品条码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="shangpinName">
        <el-input
          v-model="queryParams.shangpinName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="商品规格" prop="shangpinGuige">
        <el-input
          ref="input"
          v-model="queryParams.shangpinGuige"
          placeholder="请输入商品规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="商品类别" prop="shangpinLeibie">-->
<!--        <el-input-->
<!--          v-model="queryParams.shangpinLeibie"-->
<!--          placeholder="请输入商品类别"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="商品进货价" prop="shangpinPifajia">
        <el-input
          v-model="queryParams.shangpinPifajia"
          placeholder="请输入商品进货价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品零售价" prop="shangpinLingshoujia">
        <el-input
          v-model="queryParams.shangpinLingshoujia"
          placeholder="请输入商品零售价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="总部底价" prop="shangpinDijia">-->
<!--        <el-input-->
<!--          v-model="queryParams.shangpinDijia"-->
<!--          placeholder="请输入总部底价"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="备注" prop="beizhu">
        <el-input
          v-model="queryParams.beizhu"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item style="margin-left: 30px">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['goods:spxx:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['goods:spxx:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['goods:spxx:del']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['goods:spxx:export']"
        >导出</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shangpinxinxiList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="编号" align="center" prop="id" />-->
      <el-table-column label="商品条码" align="center" prop="shangpinId" />
      <el-table-column label="商品名称" align="center" prop="shangpinName" />

      <el-table-column label="商品规格" align="center" prop="shangpinGuige" />
<!--      <el-table-column label="商品类别" align="center" prop="shangpinLeibie" />-->
      <el-table-column label="商品进货价(￥)" align="center" prop="shangpinPifajia" >
        <template v-slot="scope">
          <span>{{scope.row.shangpinPifajia | rounding}}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品零售价(￥)" align="center" prop="shangpinLingshoujia" >
        <template v-slot="scope">
          <span>{{scope.row.shangpinLingshoujia | rounding}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="总部底价" align="center" prop="shangpinDijia" />-->
      <el-table-column label="备注" align="center" prop="beizhu" />
      <el-table-column label="商品图片" align="center" prop="shangpinTupian" >
        <template v-slot="scope">
          <div>
<!--            <img :src="scope.row.shangpinTupian" alt="" style="width: 150px;height: 150px">-->
            <image-preview slot="reference" v-if="scope.row.shangpinTupian" :src="scope.row.shangpinTupian" style="width: 150px;height: 150px"></image-preview>
          </div>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['goods:spxx:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['goods:spxx:del']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="商品条码" prop="shangpinId" class="grid-xs-7">
          <el-input v-model="form.shangpinId" placeholder="请输入商品编号" />
        </el-form-item>
        <el-form-item label="商品名称" prop="shangpinName" class="grid-xs-7">
          <el-input v-model="form.shangpinName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品规格" prop="shangpinGuige" class="grid-xs-7">
          <el-input v-model="form.shangpinGuige" placeholder="请输入商品规格" />
        </el-form-item>
<!--        <el-form-item label="商品类别" prop="shangpinLeibie" class="grid-xs-7">-->
<!--          <el-input v-model="form.shangpinLeibie" placeholder="请输入商品类别" />-->
<!--        </el-form-item>-->
        <el-form-item label="商品进货价" prop="shangpinPifajia" class="grid-xs-7">
          <el-input v-model="form.shangpinPifajia" type="number" placeholder="请输入商品进货价" />
        </el-form-item>
        <el-form-item label="商品零售价" prop="shangpinLingshoujia" class="grid-xs-7">
          <el-input v-model="form.shangpinLingshoujia" type="number" placeholder="请输入商品零售价" />
        </el-form-item>
<!--        <el-form-item label="总部底价" prop="shangpinDijia" class="grid-xs-7">-->
<!--          <el-input v-model="form.shangpinDijia" placeholder="请输入总部底价" />-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="beizhu" class="grid-xs-7">
          <el-input v-model="form.beizhu" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="商品图片" prop="shangpinTupian" class="grid-xs-7">

          <el-upload
            class="goodsimg-uploader"
            :headers="upload.headers"
            :action="upload.url"
            :show-file-list="false"
            :on-success="handleGoodsimgSuccess"
            :before-upload="beforeGoodsimgUpload">
            <img v-if="imageUrl" :src="imageUrl" class="goodsimg">
            <i v-else class="el-icon-plus goodsimg-uploader-icon" />
<!--            <div slot="tip" class="el-upload__tip" style="color:red">提示：仅允许导入.png格式文件，1920*760px！</div>-->
          </el-upload>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer grid-xs-7">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
.goodsimg-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.goodsimg-uploader .el-upload:hover {
  border-color: #409EFF;
}

.goodsimg-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.goodsimg {
  width: 178px;
  height: 178px;
  display: block;
}

</style>

<script>
import { listShangpinxinxi, getShangpinxinxi, delShangpinxinxi, addShangpinxinxi, updateShangpinxinxi } from "@/api/goods/spxx";
import { getToken } from "@/utils/auth";
import data from '@/views/system/dict/data'

export default {
  name: "Shangpinxinxi",
  data() {
    return {
      tokenjwt: {
        Authorization: window.sessionStorage.getItem("Authorization")
      },
      imageUrl: "",
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息表格数据
      shangpinxinxiList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shangpinId: null,
        shangpinName: null,
        tupian: null,
        shangpinGuige: null,
        shangpinLeibie: null,
        shangpinPifajia: null,
        shangpinLingshoujia: null,
        shangpinDijia: null,
        beizhu: null,
        shangpinTupian: null
      },


      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        // 上传的文件列表
        fileList: []
      },

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shangpinId: [
          { required: true, message: "商品编号不能为空", trigger: "blur" }
        ],
        shangpinName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        shangpinPifajia: [
          { required: true, message: "商品进货价不能为空", trigger: "blur" }
        ],
        shangpinLingshoujia: [
          { required: true, message: "商品零售价不能为空", trigger: "blur" }
        ]

      }
    };
  },
  created() {
    this.getList();
  },
  filters: {
    rounding (value) {
      return value.toFixed(2)
    }
  },
  methods: {
    /** 查询商品信息列表 */
    getList() {
      this.loading = true;
      listShangpinxinxi(this.queryParams).then(response => {
        this.shangpinxinxiList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        shangpinId: null,
        shangpinName: null,
        tupian: null,
        shangpinGuige: null,
        shangpinLeibie: null,
        shangpinPifajia: null,
        shangpinLingshoujia: null,
        shangpinDijia: null,
        beizhu: null,
        shangpinTupian: null
      };
      this.resetForm("form");
      this.imageUrl = "";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品信息";
      this.upload.fileList = [];
      this.imageUrl = "";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getShangpinxinxi(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品信息";
        if(this.form.shangpinTupian!=""&&this.form.shangpinTupian!=null) {
          this.upload.fileList = [{ url: this.form.shangpinTupian }];
          this.imageUrl = this.form.shangpinTupian;
        }
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateShangpinxinxi(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShangpinxinxi(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品？').then(function() {
        return delShangpinxinxi(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/shangpinxinxi/export', {
        ...this.queryParams
      }, `商品信息_${new Date().getTime()}.xlsx`)
    },

    beforeGoodsimgUpload(file) { // 上传了图片前的校验
      let isRightSize = file.size / 1024 / 1024 < 2
      if (!isRightSize) {
        this.$message.error('文件大小超过 2MB')
      }
      return isRightSize
    },
    // 文件提交处理
    submitUpload() {
      this.$refs.upload.submit();
    },
   // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
   // 文件上传成功处理
    handleGoodsimgSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      this.form.shangpinTupian = response.url;
      this.imageUrl = response.url;
      // this.$modal.msgSuccess(response.msg);
    },
    handleDelFileUpload(file, filelist){
      this.form.shangpinTupian="";
    }
  }
};
</script>
<style lang="scss" scoped>
@import 'src/assets/styles/distributed.scss';
</style>
